package com.guigu.pd.base.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.guigu.pd.base.common.CustomIdGenerator;
import com.guigu.pd.base.entity.TruckEntity;
import com.guigu.pd.base.service.TruckService;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import com.guigu.pd.serviceapi.basefeign.TruckFeign;
import com.guigu.pd.serviceapi.pojo.TransportTripsTruckDriver;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.base.entity.TransportTripsTruckDriverEntity;
import com.guigu.pd.base.service.TransportTripsTruckDriverService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 车次与车辆和司机关联表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:09:01
 */
@RestController
@RequestMapping("base/transporttripstruckdriver")
public class TransportTripsTruckDriverController {
    @Autowired
    private TransportTripsTruckDriverService transportTripsTruckDriverService;

    @Autowired
    private CustomIdGenerator customIdGenerator;

    @Autowired
    private TruckService truckService;

    @Autowired
    TruckFeign truckFeign;

    @Autowired
    UserFeign userFeign;


    /**
     * 给某个车次安排某些车辆
     */
    @PostMapping("/transporttripstruckdriversaveTruck")
    public R transporttripstruckdriversaveTruck(@RequestParam Map<String, Object> params){

        TransportTripsTruckDriverEntity tripstruckdriver = JSON.parseObject(JSON.toJSONString(params),
                TransportTripsTruckDriverEntity.class);

        boolean save = false;
        List<TransportTripsTruckDriverEntity> transportTripsTruckDriverlist =
                new ArrayList<TransportTripsTruckDriverEntity>();

        List<String> truckslist = tripstruckdriver.getTruckslist();
        for (String s : truckslist) {
            TransportTripsTruckDriverEntity transportTripsTruckDriverEntity = new TransportTripsTruckDriverEntity();
            transportTripsTruckDriverEntity.setTruckId(s);
            transportTripsTruckDriverEntity.setTransportTripsId(tripstruckdriver.getTransportTripsId());
            transportTripsTruckDriverlist.add(transportTripsTruckDriverEntity);
        }

        for (TransportTripsTruckDriverEntity tripsTruckDriverEntity : transportTripsTruckDriverlist) {
            tripsTruckDriverEntity.setId(customIdGenerator.nextId(tripsTruckDriverEntity)+"");

            save = transportTripsTruckDriverService.save(tripsTruckDriverEntity);
        }

        if(save){
            return R.ok("添加成功");
        }

        return R.ok("添加失败");
    }




    /**
     * 安排司机时先根据车次id查询处车次 车辆 司机信息
     */
    @GetMapping("/findtransporttripstruckdriverbycheciid/{id}")
    private List<TransportTripsTruckDriverEntity> findtransporttripstruckdriverbycheciid(@PathVariable(name = "id") String id){

        QueryWrapper<TransportTripsTruckDriverEntity> queryWrapper = new QueryWrapper<TransportTripsTruckDriverEntity>();

        queryWrapper.eq("transport_trips_id",id);

        List<TransportTripsTruckDriverEntity> list = transportTripsTruckDriverService.list(queryWrapper);

        //根据查询出来的信息去车辆表查询车辆名称
        for (TransportTripsTruckDriverEntity transportTripsTruckDriverEntity : list) {
            TruckEntity byId = truckService.getById(transportTripsTruckDriverEntity.getTruckId());

            transportTripsTruckDriverEntity.setTruckLicensePlate(byId.getLicensePlate());
        }

        return list;
    }


    /**
     * 给某一车次的某一车辆安排某一司机
     */
    @PostMapping("/transporttripstruckdriversaveTruckDriver")
    public R transporttripstruckdriversaveTruckDriver(@RequestParam Map<String, Object> params){
        TransportTripsTruckDriverEntity tripstruckdriver = JSON.parseObject(JSON.toJSONString(params),
                TransportTripsTruckDriverEntity.class);
        UpdateWrapper<TransportTripsTruckDriverEntity> updateWrapper = new UpdateWrapper<TransportTripsTruckDriverEntity>();

        updateWrapper.eq("id",tripstruckdriver.getId()).set("user_id",tripstruckdriver.getUserId());

        boolean update = transportTripsTruckDriverService.update(updateWrapper);

        if(update){
            return R.ok("添加/修改 成功");
        }

        return R.ok("添加/修改 失败");
    }

    //根据车次id去 pd_transport_trips_truck_driver 车次与车辆和司机关联表 查询数据
    @GetMapping("/queryTransportTripsTruckDriverbytransportTripsId/{transportTripsId}")
    public List<TransportTripsTruckDriver> queryTransportTripsTruckDriverbytransportTripsId(@PathVariable("transportTripsId") String transportTripsId){
        LambdaQueryWrapper<TransportTripsTruckDriverEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TransportTripsTruckDriverEntity::getTransportTripsId,transportTripsId);
        List<TransportTripsTruckDriverEntity> list = transportTripsTruckDriverService.list(lambdaQueryWrapper);
        List<TransportTripsTruckDriver> list1 = new ArrayList<>();
        BeanUtils.copyProperties(list1,list);
        return list1;
    }

    @RequestMapping("/getbyid/{id}")
    public String getinfobyId(@PathVariable("id") String id) {
        QueryWrapper<TransportTripsTruckDriverEntity> queryWrapper = new QueryWrapper<TransportTripsTruckDriverEntity>();
        queryWrapper.eq("transport_trips_id",id);
        TransportTripsTruckDriverEntity byId = transportTripsTruckDriverService.getOne(queryWrapper);
        if(byId!=null) {
            return byId.getTruckId() + "," + byId.getUserId();
        }else {
            return "";
        }
    }

    @GetMapping("/getinfo/{id}")
    public List<TransportTripsTruckDriverEntity> getinfo(@PathVariable("id") String id) {
        QueryWrapper<TransportTripsTruckDriverEntity> queryWrapper = new QueryWrapper<TransportTripsTruckDriverEntity>();
        queryWrapper.eq("transport_trips_id",id);
        List<TransportTripsTruckDriverEntity> list = transportTripsTruckDriverService.list(queryWrapper);
        for (TransportTripsTruckDriverEntity truckDriver : list) {
            String truckname = truckFeign.gettrucknamebyid(truckDriver.getTruckId());
//            String username = userFeign.getuserinfobyId(truckDriver.getUserId());
            truckDriver.setTruckname(truckname);
//            truckDriver.setUser(username);
        }
        return list;
    }

    @RequestMapping("/getuserid/{id}")
    public String getuseridbytripsid(@PathVariable("id") String id) {
        QueryWrapper<TransportTripsTruckDriverEntity> queryWrapper = new QueryWrapper<TransportTripsTruckDriverEntity>();
        queryWrapper.eq("transport_trips_id",id);
        TransportTripsTruckDriverEntity byId = transportTripsTruckDriverService.getOne(queryWrapper);
        if(byId!=null) {
            return byId.getUserId();
        }else {
            return "";
        }
    }

    @RequestMapping("/gettripsid/{id}")
    public List<String> gettripsidbyuserid(@PathVariable("id") String id) {
        QueryWrapper<TransportTripsTruckDriverEntity> queryWrapper = new QueryWrapper<TransportTripsTruckDriverEntity>();
        queryWrapper.eq("user_id",id);
        List<TransportTripsTruckDriverEntity> byId = transportTripsTruckDriverService.list(queryWrapper);
        List<String> list = new ArrayList<String>();
        for (TransportTripsTruckDriverEntity driver : byId) {
            list.add(driver.getTransportTripsId());
        }
        return list;
    }

    @RequestMapping("/gettripsbytid/{id}")
    public List<String> gettripsidbytruckId(@PathVariable("id") String id) {
        QueryWrapper<TransportTripsTruckDriverEntity> queryWrapper = new QueryWrapper<TransportTripsTruckDriverEntity>();
        queryWrapper.eq("truck_id",id);
        List<TransportTripsTruckDriverEntity> byId = transportTripsTruckDriverService.list(queryWrapper);
        List<String> list = new ArrayList<String>();
        for (TransportTripsTruckDriverEntity driver : byId) {
            list.add(driver.getTransportTripsId());
        }
        return list;
    }




}
